<template>
  <basic-container>
    <div class="avue-crud">
      <el-form :inline="true">
        <el-form-item>
          <el-button
            icon="el-icon-plus"
            type="primary"
            @click="addOrUpdateHandle(false)"
          >
            添加
          </el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" border :data="menuList" row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildrens'}">
        <el-table-column prop="name" label="名称" :show-overflow-tooltip="true" />
        <el-table-column prop="code" label="编码" />
        <el-table-column prop="lev" label="层级">
          <template slot-scope="scope">
            <el-tag type="success">{{ scope.row.lev }}级</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="parentName" label="父类" />
        <el-table-column prop="dictCode" label="字典编码" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="updateTime" label="更新时间" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="addOrUpdateHandle(true,scope.row.id)"
            >修改
            </el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList" />
    </div>
  </basic-container>
</template>

<script>
// import { doGet, doPost, doDelet, doPut } from '@/router/axios'
import { formCreate } from "@/api/index"
import TableForm from './treeform'
export default {
  name: 'DictItemTree',
  components: { TableForm },
  data() {
    return {
      addOrUpdateVisible: false,
      loading: true,
      menuList: [],
      menuOptions: []
    }
  },
  computed: {

  },
  created() {
    this.getList()
  },
  methods: {
    addOrUpdateHandle(isEdit, id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(isEdit, id)
      })
    },
    getList() {
      this.loading = true
      var params = {}
      // doGet('/sys/dictItemTree/tree', params).then(res => {
      //   this.menuList = res.data.data
      //   this.loading = false
      // })
      formCreate.dictItemTreeTree(params).then(res => {
        this.menuList = res.data
        this.loading = false
      })
    },
    handleDelete(row) {
      const that = this
      this.$confirm('是否确认删除名称为"' + row.name + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        // doDelet('/sys/dictItemTree/delete/' + row.id).then(res => {
        //   if (res.data.code === 200) {
        //     that.getList()
        //     that.$message.success('删除成功')
        //   } else {
        //     that.$message.error(res.data.msg)
        //   }
        // })
        formCreate.dictItemTreeDelete(row.id).then(res => {
          if (res.code === 200) {
            that.getList()
            that.$message.success('删除成功')
          } else {
            that.$message.error(res.msg)
          }
        })
        
      })
    }
  }
}
</script>
